layui.define(["table", "form"], function(e) {
    var admin = layui.admin;
    var $ = layui.$,
        table = layui.table,
        form = layui.form
    //事件
    var active = {
        batchdel: function() {
            var checkStatus = table.checkStatus('LAY-user-back-role'),
                checkData = checkStatus.data; //得到选中的数据
            if (checkData.length === 0) {
                return layer.msg('请选择数据');
            }
            layer.confirm('确定删除吗？', function(index) {
                var _ids = layui.getParamValues(checkData, 'r_id');
                admin.req({
                    url: layui.setter.app_url + 'roles/delete',
                    method: 'delete',
                    datatype: 'json',
                    data: {
                        "r_id": _ids
                    },
                    done: function(res) {
                        layer.msg(res.msg);
                    }
                });
                table.reload('LAY-user-back-role');
               
            });
        },
        add: function() {
            layer.open({
                type: 2,
                title: '添加新角色',
                content: layui.setter.app_url + 'roles/create',
                area: ['500px', '480px'],
                btn: ['确定', '取消'],
                yes: function(index, layero) {
                    var iframeWindow = window['layui-layer-iframe' + index],
                        submit = layero.find('iframe').contents().find("#LAY-user-role-submit");
                    //监听提交
                    iframeWindow.layui.form.on('submit(LAY-user-role-submit)', function(data) {
                        var field = data.field;
                        admin.req({
                            url: layui.setter.app_url + 'roles/save',
                            method: 'post',
                            datatype: 'json',
                            data: field,
                            done: function(res) {
                                layer.msg(res.msg);
                                if (res.code == "0") {
                                    table.reload('LAY-user-back-role');
                                    layer.close(index); //关闭弹层
                                }
                            }
                        });
                    });
                    submit.trigger('click');
                }
            });
        }
    }
    $('.layui-btn.layuiadmin-btn-role').on('click', function() {
        var type = $(this).data('type');
        active[type] ? active[type].call(this) : '';
    });
    //表格内数据
    table.render({
        elem: "#LAY-user-back-role",
        url: layui.setter.app_url + "roles/listData",
        cols: [
            [{
                type: "checkbox",
                fixed: "left"
            }, {
                field: "r_id",
                title: "ID",
                sort: !0
            }, {
                field: "r_name",
                title: "角色名"
            }, {
                field: "role_user_count",
                title: "用户数"
            }, {
                field: "r_desc",
                title: "具体描述"
            }, {
                title: "操作",
                align: "center",
                fixed: "right",
                toolbar: "#table-useradmin-admin"
            }]
        ],
        text: "对不起，加载出现异常！",
        page: !0,
        limit: 10,
        height: "full-220",
    }), table.on("tool(LAY-user-back-role)", function(e) {
        e.data;
        if ("del" === e.event) layer.confirm("确定删除此角色？", function(t) {
            admin.req({
                url: layui.setter.app_url + 'roles/delete',
                method: 'delete',
                datatype: 'json',
                data: {
                    r_id: e.data.r_id
                },
                done: function(res) {
                    layer.msg(res.msg);
                    if(res.code=="0"){
                         e.del();
                    }
                }
            });
            layer.close(t)
        });
        else if ("edit" === e.event) {
            var _id = e.data.r_id;
            layer.open({
                type: 2,
                title: "编辑角色",
                content: layui.setter.app_url + "roles/edit?id=" + _id,
                area: ["500px", "480px"],
                btn: ["确定", "取消"],
                yes: function(e, t) {
                    var l = window["layui-layer-iframe" + e],
                        r = t.find("iframe").contents().find("#LAY-user-role-submit");
                    l.layui.form.on("submit(LAY-user-role-submit)", function(t) {
                        admin.req({
                            url: layui.setter.app_url + 'roles/update?id=' + _id,
                            method: 'put',
                            datatype: 'json',
                            data: t.field,
                            done: function(res) {
                                layer.msg(res.msg);
                                if (res.code == "0") {
                                   
                                    table.reload('LAY-user-back-role');
                                    layer.close(e); //关闭弹层
                                }
                            }
                        });
                    }), r.trigger("click")
                },
                success: function(e, t) {}
            })
        }
    }), e("roles", {})
});